﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace _00365.Biz
{
	public class Employee
	{
		
		public static List<Data.Employee> GetAll(Sys.RowState rowState)
		{
			List<Data.Employee> emp;
			if (rowState == Sys.RowState.All)
			{
				emp = (from i in DBInstance.Models.Employees
					  select i).ToList();
			}
			else
			{
				emp = (from i in DBInstance.Models.Employees
					  where i.RowState.Equals(true)
					  select i).ToList();
			}

			if (emp != null)
				return emp;
			return null;
		}


		public static Data.Employee Get(Guid Id)
		{
			var item = from i in DBInstance.Models.Employees
					   where i.Id.Equals(Id)
					   select i;

			if (item != null)
				return item.FirstOrDefault();
			return null;
		}

		public static void Remove(Data.Employee employee)
		{
			employee.RowState = false;
			DBInstance.Models.SaveChanges();
		}

		public static List<Data.Employee> Search(Guid subsidiary,
												string gender,
												string position,
												string department,
												string dependent,
												string marriageStatus,
												int age,
												string schoolDegree,
												string period,
												int workingPeriod,
												string occupation,
												decimal minRevenue,
												decimal maxRevenue)
		{
			var employees = from i in DBInstance.Models.Employees
							select i;

			if (subsidiary.Equals((new Guid()).ToString()))
				employees = employees.Where(i => i.Company.Id.Equals(subsidiary));

			if (gender != "All")
				employees = employees.Where(i => i.Gender.Equals(gender));

			if (!string.IsNullOrEmpty(position))
				employees = employees.Where(i => i.Position.Equals(position));

			if (!string.IsNullOrEmpty(department))
				employees = employees.Where(i => i.Department.Equals(department));

			if (marriageStatus != "All")
				employees = employees.Where(i => i.MarriageStatus.Equals(marriageStatus));

			if (schoolDegree != "All")
				employees = employees.Where(i => i.SchoolDegree.Equals(schoolDegree));

			if (!string.IsNullOrEmpty(period))
				employees = employees.Where(i => i.Period.Equals(period));
			
			if (workingPeriod > 0)
			{
				int workingYears = DateTime.Today.Year - workingPeriod;
				DateTime startDate = new DateTime(workingYears, DateTime.Today.Month, DateTime.Today.Day);
				employees = employees.Where(i => i.HiringDate <= startDate);
			}

			if (!string.IsNullOrEmpty(occupation))
				employees = employees.Where(i => i.Period.Equals(occupation));

			if (age > 0)
			{
				int BornYear = DateTime.Today.Year - age;
				DateTime startDate = new DateTime(BornYear - 1, DateTime.Today.Month, DateTime.Today.Day + 1);
				DateTime endDate = new DateTime(BornYear, DateTime.Today.Month, DateTime.Today.Day);
				employees = employees.Where(i => i.BirthDate >= startDate).Where(i => i.BirthDate <= endDate);
			}

			if (dependent != "All")
			{
				if (dependent == "True")
					employees = employees.Where(i => i.fnc_EmployeeDependent.Count > 0);
				else
					employees = employees.Where(i => i.fnc_EmployeeDependent.Count == 0);
			}

			if (minRevenue > 0)
				employees = employees.Where(i => i.Revenue >= minRevenue);

			if (maxRevenue > 0)
				employees = employees.Where(i => i.Revenue <= maxRevenue);

			return employees.ToList();
		}


        public static void Add(Data.Employee employee, HttpSessionState Session)
        {
            Sys.SetBasicObjectProperties(employee, true, true, Session);
            DBInstance.Models.AddToEmployees(employee);
            DBInstance.Models.SaveChanges();        
        }

        public static void Update(Data.Employee employee, HttpSessionState Session)
        {
            Sys.SetBasicObjectProperties(employee, false, true, Session);
            DBInstance.Models.SaveChanges();
        }
    }
}